//
//  FirstEnterAppViewController.m
//  txlc_ios
//
//  Created by ayi on 2018/11/30.
//  Copyright © 2018 txlc. All rights reserved.
//

#import "FirstEnterAppViewController.h"

@interface FirstEnterAppViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *images;


@end

@implementation FirstEnterAppViewController

- (NSArray *)images
{
    if (!_images) {
        
        if (IS_IPHONE_4_4s) {
            _images = @[@"750x1334_01.jpeg",@"750x1334_02.jpeg",@"750x1334_03.jpeg",@""];
        }else if (IS_IPHONE_5_5s_5c){
            _images = @[@"750x1334_01.jpeg",@"750x1334_02",@"750x1334_03.jpeg",@""];
        }else if (IS_IPHONE_6_6s_7_8){
            _images = @[@"750x1334_01.jpeg",@"750x1334_02.jpeg",@"750x1334_03.jpeg",@""];
        }else if (IS_IPHONE_6p_6sp_7p_8p){
            _images = @[@"750x1334_01.jpeg",@"750x1334_02.jpeg",@"750x1334_03.jpeg",@""];
        }else{
            _images = @[@"750x1334_01.jpeg",@"750x1334_02.jpeg",@"750x1334_03.jpeg",@""];
        }
        
    }
    
    return _images;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = self.view.bounds.size;
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView = collectionView;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.pagingEnabled = YES;
    collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView registerClass:[FirstEnterAppCell class] forCellWithReuseIdentifier:@"FirstEnterAppCell"];
    collectionView.bounces = NO;
    [self.view addSubview:collectionView];
    
}

#pragma UICollectionView DataSource


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.images.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    FirstEnterAppCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FirstEnterAppCell" forIndexPath:indexPath];
    cell.imageUrl = self.images[indexPath.row];
    [cell setBtnCallBack:^{
        
        [[NSUserDefaults standardUserDefaults] setObject:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] forKey:@"caruser_version"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        BaseTabBarViewController *tabbar = [[BaseTabBarViewController alloc] init];
        [UIApplication sharedApplication].keyWindow.rootViewController = tabbar;
    }];
    if (indexPath.row == self.images.count - 1) {
        cell.btn.hidden = NO;
    }else{
        cell.btn.hidden = YES;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ((self.images.count-1) == indexPath.row) {
        
    }
    
}

@end






@interface FirstEnterAppCell()

@property (nonatomic, weak) UIImageView *imageView;

@end


@implementation FirstEnterAppCell


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)decoder {
    if (self = [super initWithCoder:decoder]) {
        [self setup];
    }
    return self;
}

- (void)setup
{
    UIImageView *imageView = [[UIImageView alloc] init];
    self.imageView = imageView;
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:imageView];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
//    UIImage *img = IMG(@"btn");
    _btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [_btn setImage:IMG(@"btn") forState:UIControlStateNormal];
    _btn.layer.masksToBounds = YES;
    _btn.layer.cornerRadius = 8;
    _btn.layer.borderColor = Common_TextColor.CGColor;
    _btn.layer.borderWidth = 0.5f;
    [_btn setTitle:@"进入商城" forState:UIControlStateNormal];
    [_btn setTitleColor:Common_TextColor forState:UIControlStateNormal];
    [self.contentView addSubview:_btn];
    [_btn mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.centerX;
        make.height.mas_equalTo(44);
        make.width.mas_equalTo(100);
        make.bottom.mas_equalTo((IS_IPHONE_X || IS_IPHONE_6p_6sp_7p_8p)?-80:(IS_IPHONE_6_6s_7_8?-70:(IS_IPHONE_5_5s_5c?-60:-50)));
    }];
    _btn.hidden = YES;
    [_btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)btnAction{
    if (self.btnCallBack) {
        self.btnCallBack();
    }
}


- (void)setImageUrl:(NSString *)imageUrl
{
    _imageUrl = imageUrl.copy;
    
    self.imageView.image = [UIImage imageNamed:imageUrl];
}



@end
